/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author 张丁方
 * @CreateTime 2017-3-29 15:20:59
 */
package com.hyyt.shopjsp.product.service.imp;

import com.hyyt.shopjsp.product.dao.ShopProductMeasuringUnitDAO;
import com.hyyt.shopjsp.product.pojo.ShopProductMeasuringUnit;
import com.hyyt.shopjsp.product.service.IShopProductMeasuringUnitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.apache.log4j.Logger;

@Service
public class ShopProductMeasuringUnitService implements IShopProductMeasuringUnitService {
    private static  final Logger logger = Logger.getLogger(IShopProductMeasuringUnitService.class);

    @Resource
    private ShopProductMeasuringUnitDAO shopProductMeasuringUnitDAO;

    /**
     * 保存SHOP_商品计量单位
     *
     * @param shopProductMeasuringUnit SHOP_商品计量单位
     * @return 保存后的SHOP_商品计量单位
     */
    @Override
    public void saveShopProductMeasuringUnit(ShopProductMeasuringUnit shopProductMeasuringUnit) {
        shopProductMeasuringUnitDAO.insert(shopProductMeasuringUnit);
    }

    /**
     * 更新SHOP_商品计量单位
     *
     * @param shopProductMeasuringUnit SHOP_商品计量单位
     * @return 更新后的SHOP_商品计量单位
     */
    @Override
    public void updateShopProductMeasuringUnit(ShopProductMeasuringUnit shopProductMeasuringUnit) {
        shopProductMeasuringUnitDAO.update(shopProductMeasuringUnit);
    }

    /**
     * 删除指定ID的SHOP_商品计量单位
     *
     * @param id SHOP_商品计量单位ID
     */
    @Override
    public void deleteShopProductMeasuringUnit(Integer id) {
        shopProductMeasuringUnitDAO.delete(id);
    }


    /**
     * 删除指定计量单位ID的SHOP_商品计量单位
     *
     * @param id SHOP_商品计量单位ID
     */
    @Override
    public void deleteShopProductMeasuringUnitByMeasuringUnitId(Integer id) {
        shopProductMeasuringUnitDAO.deleteByMeasuringUnitId(id);
    }

    /**
     * 删除指定ID集合的SHOP_商品计量单位
     *
     * @param ids SHOP_商品计量单位ID集合
    */
    @Override
    public void deleteShopProductMeasuringUnit(Collection<Integer> ids){
        shopProductMeasuringUnitDAO.batchDelete(ids);
    }

    /**
     * 根据ID加载一个SHOP_商品计量单位
     *
     * @param id SHOP_商品计量单位ID
     * @return SHOP_商品计量单位
     */
    @Override
    public ShopProductMeasuringUnit loadShopProductMeasuringUnit(Integer id) {
        return shopProductMeasuringUnitDAO.load(id);
    }

    /**
    * 查询所有的SHOP_商品计量单位，通常有最多取30000条记录的限制
    *
    * @return 所有的SHOP_商品计量单位，通常有最多取30000条记录的限制
    */
    @Override
    public List<ShopProductMeasuringUnit> queryAllShopProductMeasuringUnit() {
        return shopProductMeasuringUnitDAO.queryAll();
    }

    /**
     * 查询SHOP_商品计量单位
     *
     * @param params 查询参数，为key-value形式
     * @return SHOP_商品计量单位集合
     */
    @Override
    public List<ShopProductMeasuringUnit> queryShopProductMeasuringUnit(Map<String, Object> params) {
        return shopProductMeasuringUnitDAO.query(params);
    }

    /**
     * 查询SHOP_商品计量单位
     *
     * @param params   查询参数，没有则传null,分页参数key为“sortColumns”
     * @param startRow 起始行数（不含起始行的数据）
     * @param rowSize  要查询记录数
     * @return SHOP_商品计量单位集合
     */
    @Override
    public List<ShopProductMeasuringUnit> queryShopProductMeasuringUnit(Map<String, Object> params, int startRow, int rowSize) {
        return shopProductMeasuringUnitDAO.query(params, startRow, rowSize);
    }

    /**
     * 统计SHOP_商品计量单位
     *
     * @param params 查询参数，没有则传null,分页参数key为“sortColumns”
     * @return 结果集记录数
     */
    @Override
    public int countShopProductMeasuringUnit(Map<String, Object> params) {
        return shopProductMeasuringUnitDAO.count(params);
    }

    /**
     * 根据计量单位ID和商品分类ID删除商品分类下的计量单位
     *
     * @param productTypeId 商品分类id
     * @param measuringUnitId 计量单位id
     */
    @Override
    public void deleteMeasuringUnitByProductTypeIdAndMeasuringUnitId(Integer productTypeId, Integer measuringUnitId) {
        Map<String, Object> param = new HashMap<>();
        param.put("productTypeId",productTypeId);
        param.put("measuringUnitId",measuringUnitId);
        shopProductMeasuringUnitDAO.delete("deleteMeasuringUnitByProductTypeIdAndMeasuringUnitId",param);
    }

    /**
     * 根据计量单位ID和商品分类ID添加新的计量单位给商品分类
     *
     * @param productTypeId 商品分类id
     * @param measuringUnitIds  计量单位id 拼串字符
     * @return 保存后的SHOP_计量单位
     */
    @Override
    public void saveShopMeasuringUnit(Integer productTypeId, String measuringUnitIds) {
        String [] measuringUnitIdsStr=measuringUnitIds.split(",");
        for (int i=0; i< measuringUnitIdsStr.length;i++) {
            ShopProductMeasuringUnit shopProductMeasuringUnit = new ShopProductMeasuringUnit();
            shopProductMeasuringUnit.setProductTypeId(productTypeId);
            shopProductMeasuringUnit.setMeasuringUnitId(Integer.parseInt(measuringUnitIdsStr[i]));
            shopProductMeasuringUnitDAO.insert(shopProductMeasuringUnit);
        }
    }

    /**
     * 根据商品分类ID获取计量单集合信息
     *
     * @param productTypeId 商品分类id
     */
    @Override
    public List<Map<String,Object>> queryMeasuringUnitListByProductTypeId(Integer productTypeId) {
        Map<String, Object> params = new HashMap<>();
        params.put("productTypeId",productTypeId);
        return shopProductMeasuringUnitDAO.query("queryMeasuringUnitListByProductTypeId",params);
    }
}